﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.Remoting;
using Spring.Context.Support;
using ESPlus.Widgets;
using Spring.Context;
using ESFramework;

namespace ESFDemo.Server
{
    static class Program
    {
        public static IApplicationContext SpringContext;
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                //如果是其它类型的授权用户，请使用下面的语句设定正确的授权用户ID和密码。
                AuthorizationVerifier.SetAuthorizedUser(AuthorizationVerifier.FreeUser, "");
                
                Program.SpringContext = ContextRegistry.GetContext();
                Program.ConfigEsbLogger();

                MainServerForm mainForm = (MainServerForm)Program.SpringContext.GetObject("mainServerForm");
                Application.Run(mainForm);
            }
            catch (Exception ee)
            {
                ee = ee;

                if (ee is System.Configuration.ConfigurationException)
                {
                    while (ee.InnerException != null)
                    {
                        ee = ee.InnerException;
                    }

                    MessageBox.Show("配置初始化失败：" + ee.Message);
                }
                else
                {
                    MessageBox.Show(ee.Message);
                }
            }
        }

        #region ConfigEsbLogger
        private static void ConfigEsbLogger()
        {
            IList<object> objList = new List<object>();
            string[] names = Program.SpringContext.GetObjectDefinitionNames();
            foreach (string name in names)
            {
                objList.Add(Program.SpringContext.GetObject(name));
            }

            object esfLogger = Program.SpringContext.GetObject("esfLogger");
            ESBasic.Helpers.ReflectionHelper.SetProperty(objList, "EsfLogger", esfLogger);
        }
        #endregion
    }
}
